home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / PositionableStream.st < prev    next >
Text File  |  1991-09-12  |  5KB  |  201 lines

  1. "======================================================================
  2. |
  3. |   PositionableStream Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Converted to use real method categories
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. Stream subclass: #PositionableStream
  40.        instanceVariableNames: 'collection ptr endPtr access'
  41.        classVariableNames: ''
  42.        poolDictionaries: ''
  43.        category: nil.
  44.  
  45. PositionableStream comment: 
  46. 'My instances represent streams where explicit positioning is permitted.
  47. Thus, my streams act in a manner to normal disk files: you can read
  48. or write sequentially, but also position the file to a particular place
  49. whenever you choose.  Generally, you''ll want to use ReadStream, WriteStream
  50. or ReadWriteStream instead of me to create and use streams.' !
  51.  
  52.  
  53. !PositionableStream class methodsFor: 'instance creation'!
  54.  
  55. on: aCollection
  56.     self subclassResponsiblity
  57. !
  58.  
  59. on: aCollection from: firstIndex to: lastIndex
  60.     ^self on: (aCollection copyFrom: firstIndex to: lastIndex)
  61.  
  62. !!
  63.  
  64.  
  65.  
  66. !PositionableStream methodsFor: 'accessing-reading'!
  67.  
  68. next
  69.     | element |
  70.     (access bitAnd: 1) = 0
  71.         ifTrue: [ ^self shouldNotImplement ].
  72.     self atEnd ifTrue: [ ^self error: 'end of stream reached' ].
  73.     element _ collection at: ptr.
  74.     ptr _ ptr + 1.
  75.     ^element
  76. !
  77.  
  78. next: anInteger
  79.     "Returns a collection of the same type that the stream accesses, that has
  80.     the next anInteger elements from the stream."
  81.     | newStream |
  82.     newStream _ WriteStream on: (collection species new: 0).
  83.     anInteger timesRepeat: [ newStream nextPut: (self next) ].
  84.     ^newStream contents
  85. !
  86.  
  87. peek
  88.     "Returns the next element of the stream without moving the pointer.
  89.     Returns nil when at end of stream."
  90.     | peekValue |
  91.     self atEnd ifTrue: [ ^nil ].
  92.     peekValue _ self next.
  93.     self skip: -1.
  94.     ^peekValue    
  95. !
  96.  
  97. peekFor: anObject
  98.     "Returns true and gobbles the next element from the stream of it is
  99.     equal to anObject, returns false and doesn't gobble the next element
  100.     if the next element is not equal to anObject."
  101.     (self peek) = anObject
  102.         ifTrue: [ self next.
  103.               ^true ]
  104.         ifFalse: [ ^false ]
  105. !
  106.  
  107. upTo: anObject
  108.     "Returns a collection of the same type that the stream accesses, up to 
  109.     but not including the object anObject.  Returns the entire rest of the 
  110.     stream's contents if anObject is not present.  ### Should this use = or
  111.     ==."
  112.     | newStream |
  113.     newStream _ WriteStream on: (collection species new: 0).
  114.     [ self atEnd or: [ self peek ~= anObject ] ] whileFalse:
  115.         [ newStream nextPut: (self next) ].
  116.     ^newStream contents
  117. !
  118.  
  119. contents
  120.     "Returns a collection of the same type that the stream accesses, up to 
  121.     and including the final element."
  122.     ^collection copyFrom: 1 to: endPtr
  123. !
  124.  
  125. reverseContents
  126.     "Returns a collection of the same type that the stream accesses, up to 
  127.     and including the final element, but in reverse order."
  128.     | newCollection |
  129.     newCollection _ collection species new: endPtr.
  130.     1 to: endPtr do:
  131.         [ :i | newCollection at: i put: (collection at: endPtr - i + 1) ].
  132.     ^newCollection
  133. !!
  134.  
  135.  
  136.  
  137. !PositionableStream methodsFor: 'testing'!
  138.  
  139. atEnd
  140.     ^ptr > endPtr
  141. !
  142.  
  143. isEmpty
  144.     ^endPtr = 0
  145. !!
  146.  
  147.  
  148.  
  149. !PositionableStream methodsFor: 'enumerating'!
  150.  
  151. !
  152.  
  153.  
  154.  
  155. !PositionableStream methodsFor: 'positioning'!
  156.  
  157. position
  158.     ^ptr
  159. !
  160.  
  161. position: anInteger
  162.     (anInteger between: 1 and: endPtr)
  163.     ifTrue: [ ptr _ anInteger ]
  164.     ifFalse: [ ^self error: 'position out of range' ]
  165. !
  166.  
  167. reset
  168.     ptr _ 1
  169. !
  170.  
  171. setToEnd
  172.     ptr _ endPtr + 1
  173. !
  174.  
  175. skip: anInteger
  176.     ptr _ (ptr + anInteger max: 1) min: endPtr
  177. !
  178.  
  179. skipTo: anObject
  180.     "Moves the current position to after the next occurrence of anObject
  181.     and returns true if anObject was found.  If anObject doesn't exist, the 
  182.     position is unchanged, and false is returned."
  183.     | curPos |
  184.     curPos _ self position.
  185.     [ self atEnd ] whileFalse:
  186.         [ (self nextMatchFor: anObject)
  187.         ifTrue: [ ^true ] ].
  188.     self position: curPos.
  189.     ^false    
  190.  
  191. !!
  192.  
  193.  
  194. !PositionableStream methodsFor: 'private'!
  195.  
  196. access: anInteger
  197.     access _ anInteger
  198. !!
  199.